package com.cx.service.impl;

import com.cx.mapper.CartsMapper;
import com.cx.pojo.Carts;
import com.cx.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description
 * @Author 11597
 * @Date 2020/5/13 21:01
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartsMapper cartsMapper;
    @Override
    public int updateCart(Carts carts) {

        return cartsMapper.updateByPrimaryKey(carts);
    }

    @Override
    public int save(Carts carts) {
        return cartsMapper.insert(carts);
    }

    @Override
    public int add(int id) {
        Carts carts = cartsMapper.selectByPrimaryKey(id);
        carts.setGoodNum(carts.getGoodNum()+1);
        return cartsMapper.updateByPrimaryKey(carts);
    }

    @Override
    public int less(int id) {
        Carts carts = cartsMapper.selectByPrimaryKey(id);
        if(carts.getGoodNum()<=1) {
            return delete(id);
        }
        carts.setGoodNum(carts.getGoodNum()-1);
        return cartsMapper.updateByPrimaryKey(carts);
    }

    @Override
    public int delete(int id) {

        return cartsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int clean(Integer userid) {

        return cartsMapper.deleteByUserId(userid);
    }

    @Override
    public int deleteByUserId(int userid) {
        return cartsMapper.deleteByUserId(userid);
    }

    @Override
    public Carts selectByUserIdAndGoodId(int userId, int goodId) {
        return cartsMapper.selectByUserIdAndGoodId(userId,goodId);
    }

}
